package net.homelinux.chaoswg.io.renga.core.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import net.homelinux.chaoswg.io.renga.core.gui.operations.CompositeOperation;
import net.homelinux.chaoswg.io.renga.core.gui.operations.Control;

public class GUIMenuBar extends JMenuBar {
    GUI parent = null;

    JMenuItem jmiNew, jmiOpen, jmiSave, jmiSaveAs, jmiExit;

    public GUIMenuBar (GUI parent) {
        this.parent = parent;

        addMenus();
        addListeners ();
    }

    private void addMenus () {
        JMenu jmFile= new JMenu ("File");

        jmFile.add(jmiNew = new JMenuItem ("New"));
        jmFile.add(jmiOpen = new JMenuItem ("Open..."));
        jmFile.add(jmiSave = new JMenuItem ("Save"));
        jmFile.add(jmiSaveAs = new JMenuItem ("Save as..."));
        jmFile.addSeparator();
        jmFile.add(jmiExit = new JMenuItem ("Exit"));

        this.add (jmFile);
    }

    private void addListeners () {
        jmiNew.addActionListener(new ActionListener () {
            public void actionPerformed (final ActionEvent ae) {
                new GUI(new CompositeOperation(new Control ()));
            }
        });
        jmiOpen.addActionListener(new ActionListener () {
            public void actionPerformed (final ActionEvent ae) {
                parent.open();
            }
        });
        jmiSave.addActionListener(new ActionListener () {
            public void actionPerformed (final ActionEvent ae) {
                parent.save();  
            }
        });
        jmiSaveAs.addActionListener(new ActionListener () {
            public void actionPerformed (final ActionEvent ae) {
                parent.saveAs();
            }
        });
        jmiExit.addActionListener(new ActionListener () {
            public void actionPerformed (final ActionEvent ae) {
                parent.exit ();
            }
        });
    }

}
